
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div id="lineTab" class="tab-pane">
	<div class="panel-body" style="padding-bottom: 0px; border-bottom: 0px;">

		<div class="col-lg-12 text-right" style="padding-right: 0px;">
			<c:if test="${param.approveStatus=='APPROVE'}">
				<button id="createTaskButton" class="btn btn-info btn-sm btn-notcontrol" type="button" ><i class="fa fa-arrow-circle-down"></i>&nbsp;&nbsp;<span class="bold redragon-line-button-title">下达生产任务</span></button>
				<button id="createPoaButton" class="btn btn-warning btn-sm btn-notcontrol" type="button" ><i class="fa fa-arrow-circle-down"></i>&nbsp;&nbsp;<span class="bold redragon-line-button-title">下达采购计划</span></button>
			</c:if>
			<button id="addButton" class="btn btn-info btn-sm" type="button" ><i class="fa fa-plus"></i>&nbsp;&nbsp;<span class="bold redragon-line-button-title">新增计划行</span></button>
			<%-- 
			<button id="searchButton" class="btn btn-default btn-sm" type="button"><i class="fa fa-search"></i>&nbsp;&nbsp;展开查询</button>
			--%>
		</div><br/>

		<div class="table-responsive">
			<table class="table table-stripped table-hover table-bordered border-top">

				<thead>
					<tr>
						<th></th>
						<th width="5%">行号</th>
						<th>物料编码</th>
						<th>物料名称</th>
						<th>单位</th>
						<th>规格</th>
						<th>物料属性</th>
						<th>计划数量</th>
						<th>实际需求数量</th>
						<th>库存量</th>
						<th>预计接收量</th>
						<th>计划开工日期</th>
						<th>计划完工日期</th>
						<th>下达状态</th>
						<th width="10%">操作</th>
					</tr>
				</thead>
				<tbody>
					
					<c:forEach items="${requestScope.prodMrpLineList}" var="data" varStatus="status">
					<tr>
						<td><input type="checkbox" class="i-checks" name="input[]" poaLineCode="${data.poaLineCode}" wipHeadCode="${data.wipHeadCode}"></td>
						<td class="mrpLineId" style="display: none;">${data.mrpLineId}</td>
						<td class="bomProperty" style="display: none;">${data.bomProperty}</td>
						<td>${status.count}</td>
						<td>${data.materialCode}</td>
						<td>${data.materialName}</td>
						<td>${data.materialUnit}</td>
						<td>${data.materialStandard}</td>
						<td>${requestScope.bomPropertyMap[data.bomProperty]}</td>
						<td class="lineQuantity" style="color: #1c84c6 !important;">${data.planQuantity}</td>
						<td style="color: #1c84c6 !important;">${data.needQuantity}</td>
						<td>${data.stockQuantity}</td>
						<td>${data.willInputQuantity}</td>
						<td><fmt:formatDate value="${data.planStartDate}" pattern="yyyy-MM-dd"/></td>
						<td><fmt:formatDate value="${data.planEndDate}" pattern="yyyy-MM-dd"/></td>
						<td>
							<c:if test="${data.poaLineCode!=null&&data.poaLineCode!=''}">
								<span class="label label-warning">采购已下达</span>
							</c:if>
							<c:if test="${data.wipHeadCode!=null&&data.wipHeadCode!=''}">
								<span class="label label-info">生产已下达</span>
							</c:if>
						</td>
						<td>
							<div class="btn-group">
								<c:if test="${data.poaLineCode==null||data.poaLineCode==''}">
									<button class="btn-white btn btn-xs" onclick="editData(${data.mrpLineId})"><i class="fa fa-edit"></i>&nbsp;编辑</button>&nbsp;
									<button class="btn-white btn btn-xs" onclick="deleteData(${data.mrpLineId})"><i class="fa fa-trash"></i>&nbsp;删除</button>
								</c:if>
							</div>
						</td>
					</tr>
					</c:forEach>
					
				</tbody>
				<tfoot>
					<%-- 导入页码 --%>
					<jsp:include page="../../common/pages.jsp"></jsp:include>
				</tfoot>
			</table>

		</div>

	</div>
</div>

<div id="addLineModal"></div>

<script>
	$(document).ready(function() {
		//设置已生成poa的禁用
		$('.i-checks').each(function(){
			var poaLineCode = $(this).attr("poaLineCode");
			var wipHeadCode = $(this).attr("wipHeadCode");
			
			if(poaLineCode!=null&&poaLineCode!=""){
				//$(this).iCheck('disable');
				$(this).remove();
			}
			
			if(wipHeadCode!=null&&wipHeadCode!=""){
				//$(this).iCheck('disable');
				$(this).remove();
			}
		});
		
		//初始化checkbox
		$('.i-checks').iCheck({
			checkboxClass : 'icheckbox_square-green',
			radioClass : 'iradio_square-green',
		});
		
		//checkbox选中效果
		$("tr").click(function(){
			if($(this).find(".i-checks").prop("checked")){
				$(this).find(".i-checks").iCheck('uncheck');
			}else{
				//$(".i-checks").iCheck('uncheck');
				$(this).find(".i-checks").iCheck('check');
			}
		});
		
		//添加行
		$("#addButton").click(function(){
			getLineModal();
		});
		
		//下达采购计划
		$("#createPoaButton").click(function(){
			redragonJS.confirm("确认下达采购计划？", function(){
				var selectFlag = "N"
				var poFlag = "Y"
				var mrpLineId =  "";
			
				$('.i-checks').each(function(){
					if($(this).prop("checked")){
						selectFlag = "Y";
						mrpLineId = mrpLineId+":"+$(this).parents("tr").find("td.mrpLineId").text();
						if($(this).parents("tr").find("td.bomProperty").text()!="PO"){
							poFlag = "N";
						}
					}
				});
				
				if(selectFlag=="N"){
					redragonJS.alert("必须选择至少一个MRP计划行");
				}else if(poFlag=="N"){
					redragonJS.alert("不能选择非采购的计划行");
				}else{
					redragonJS.loading("ibox-content1");
					$.ajax({
						type: "post",
						url: "web/prodMrpLine/autoCreatePoa",
						data: {"mrpLineId": mrpLineId, "mrpHeadCode": "${param.mrpHeadCode}"},
						async: false,
						dataType: "json",
						cache: false,
						success: function(data){
							redragonJS.removeLoading("ibox-content1");
							
							if(data.errCode==0){
								redragonJS.close();
								getLineTab("${param.mrpHeadCode}");
								window.open("web/poAgreementHead/getPoHead?poHeadId="+data.poHeadId+"&poHeadCode="+data.poHeadCode);
							}else{
								redragonJS.alert(data.errMsg);
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							redragonJS.alert(textStatus);
						}
					});
					
				}
			});
		});
		
		//下达生产任务
		$("#createTaskButton").click(function(){
			redragonJS.confirm("确认下达生产任务？", function(){
				var selectFlag = "N"
				var makeFlag = "Y"
				var mrpLineId =  "";
			
				$('.i-checks').each(function(){
					if($(this).prop("checked")){
						selectFlag = "Y";
						if(mrpLineId==""){
							mrpLineId = $(this).parents("tr").find("td.mrpLineId").text();
						}else{
							mrpLineId = mrpLineId+":"+$(this).parents("tr").find("td.mrpLineId").text();
						}
						
						if($(this).parents("tr").find("td.bomProperty").text()!="MAKE"){
							makeFlag = "N";
						}
					}
				});
				
				if(selectFlag=="N"){
					redragonJS.alert("必须选择一个MRP计划行");
				}else if(makeFlag=="N"){
					redragonJS.alert("不能选择非自制的计划行");
				}else if(mrpLineId.indexOf(":")!=-1){
					redragonJS.alert("生产任务下达不能选择多个计划行");
				}else{
					redragonJS.loading("ibox-content1");
					$.ajax({
						type: "post",
						url: "web/prodMrpLine/autoCreateWipTask",
						data: {"mrpLineId": mrpLineId, "mrpHeadCode": "${param.mrpHeadCode}"},
						async: false,
						dataType: "json",
						cache: false,
						success: function(data){
							redragonJS.removeLoading("ibox-content1");
							
							if(data.errCode==0){
								redragonJS.close();
								getLineTab("${param.mrpHeadCode}");
								window.open("web/prodWipTask/getProdWipTask?taskId="+data.taskId+"&taskCode="+data.taskCode);
							}else{
								redragonJS.alert(data.errMsg);
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){
							redragonJS.alert(textStatus);
						}
					});
				}
			});
		});
		
	});
	
	function editData(id){
		getLineModal(id);
	}
	
	function deleteData(id) {
		redragonJS.confirm("确认删除数据？", function(){
			deleteLine(id);
		});
	}
	
	function getLineModal(id){
		$.ajax({
			type: "post",
			url: "web/prodMrpLine/getProdMrpLine",
			data: {"mrpLineId": id, "mrpHeadCode": "${param.mrpHeadCode}"},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#addLineModal").html(data);
					$('#addLineDiv').modal('show');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	function deleteLine(id){
		$.ajax({
			type: "post",
			url: "web/prodMrpLine/deleteProdMrpLine",
			data: {"mrpLineId": id, "mrpHeadCode": "${param.mrpHeadCode}"},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				var json = JSON.parse(data);
				if(json.result=="success"){
					redragonJS.close();
					getLineTab("${param.mrpHeadCode}");
				}else{
					redragonJS.alert("删除行错误");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
</script> 